﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication.Utils;

namespace SilverlightApplication.Components
{
    public class Ballon
    {
        public Ellipse Circle { set; get; }

        public Storyboard Anime { set; get; }

        private double _opacity = 0.0;

        public delegate void ParentChangeHandler(object sender, Panel oldParent, Panel newParent);

        public event ParentChangeHandler ParentChanged;

        private Panel _parent;
        public Panel Parent
        {
            get
            {
                return _parent;
            }
            set
            {
                if (_parent == value)
                    return;
                if (_parent != null)
                {
                    _parent.Children.Remove(Circle);
                    ParentChanged(this, _parent, value);
                }
                if (value != null)
                {
                    value.Children.Add(Circle);
                    ShowAnimation();
                }
                _parent = value;
            }
        }

        private double _radius;
        public double Radius
        {
            get
            {
                return _radius;
            }
            set
            {
                if (_radius == value)
                    return;
                Circle.Width = Circle.Height = value * 2;
                _radius = value;
            }
        }

        private double _left;
        public double Left
        {
            get
            {
                return _left;
            }
            set
            {
                if (_left == value)
                    return;
                _left = value;
                Canvas.SetLeft(Circle, _left);
            }
        }

        private double _top;
        public double Top
        {
            get
            {
                return _top;
            }
            set
            {
                if (_top == value)
                    return;
                _top = value;
                Canvas.SetTop(Circle, _top);
            }
        }

        public Ballon()
        {
            Circle = new Ellipse();
            Radius = 0;
            _left = 0;
            _top = 0;
        }

        public void CreatwithRandomRadius(int minRadius, int maxRadius)
        {
            int randomRadius = 0;
            do 
            {
                randomRadius = RandomUtils.Random.Next(maxRadius);
            } while (minRadius < randomRadius);

            Radius = 15;
            Circle.Fill = new SolidColorBrush(Colors.White);
            Circle.Opacity = 0;
            _opacity = RandomUtils.Random.NextDouble();
            PlaneProjection pp = new PlaneProjection();
            pp.CenterOfRotationX = 0.5;
            pp.CenterOfRotationY = 0.5;
            pp.CenterOfRotationZ = 0.5;
            pp.GlobalOffsetZ =  -randomRadius * 1000;
            Circle.Projection = pp;

            InitClickEvent();
            CreateShowAnimation(null, null);
        }

        private void InitClickEvent()
        {
            Circle.MouseEnter += new MouseEventHandler(Circle_MouseEnter);
        }

        private void Circle_MouseEnter(object sender, MouseEventArgs e)
        {
            Circle.MouseEnter -= Circle_MouseEnter;
            Anime.Begin();
        }

        private void CreateShowAnimation(object sender, EventArgs e)
        {
            TransformGroup transformGroup = new TransformGroup();
            ScaleTransform scaleTransform = new ScaleTransform();
            transformGroup.Children.Add(scaleTransform);
            Circle.RenderTransformOrigin = new Point(0.5, 0.5);
            Circle.RenderTransform = transformGroup;
            // Create a duration of 2 seconds.
            Duration duration = new Duration(TimeSpan.FromSeconds(RandomUtils.Random.Next(7)));

            // Create two DoubleAnimations and set their properties.
            DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
            DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();
            DoubleAnimation myDoubleAnimation3 = new DoubleAnimation();
            
            myDoubleAnimation1.Duration = duration;
            myDoubleAnimation2.Duration = duration;
            myDoubleAnimation3.Duration = duration;

            Anime = new Storyboard();
            Anime.Duration = duration;

            Anime.Children.Add(myDoubleAnimation1);
            Anime.Children.Add(myDoubleAnimation2);
            Anime.Children.Add(myDoubleAnimation3);

            Storyboard.SetTarget(myDoubleAnimation1, Circle);
            Storyboard.SetTarget(myDoubleAnimation2, Circle);
            Storyboard.SetTarget(myDoubleAnimation3, Circle);

            // Set the attached properties of Canvas.Left and Canvas.Top
            // to be the target properties of the two respective DoubleAnimations.
            Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(FrameworkElement.Projection).(PlaneProjection.GlobalOffsetX)"));
            Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(FrameworkElement.Projection).(PlaneProjection.GlobalOffsetY)"));
            Storyboard.SetTargetProperty(myDoubleAnimation3, new PropertyPath("(Opacity)"));

            myDoubleAnimation1.To = 1900 * RandomUtils.Random.NextDouble();
            myDoubleAnimation2.To = 1600 * RandomUtils.Random.NextDouble();
            myDoubleAnimation3.To = 0;

            Anime.Completed += new EventHandler(Anime_Completed);
            // Make the Storyboard a resource.
            //LayoutRoot.Resources.Add("unique_id", Anime);

            // Begin the animation.
            //Anime.Begin();
        }

        private void Anime_Completed(object sender, EventArgs e)
        {
            Parent = null;
        }

        private void ShowAnimation()
        {
            StoryboardManager sm = new StoryboardManager(Circle);
            sm.
        }
    }
}
